﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Cemit.NetFramework;
using Google.Protobuf;
using Google.Protobuf.Reflection;
using System.Linq;
using Cemit.PolyProto;
using UnityEditor;
using System.Reflection;

namespace Cemit.NetFramework.Demo.Tutorial4
{
    [System.Obsolete("已过时")]
    public class NetworkManager : MonoBehaviour
    {
        public PolyPlayer playerPrefabs;
        public PolyServerSettings serverSettings;

        void Start()
        {
            PolyNetwork.OnConnectionSucceeded += OnConnected;
            //使用带有同步按键的设置连接服务器
            PolyNetwork.Connect(serverSettings);
        }

        private void OnConnected()
        {
            PolyNetwork.OnCreatePlayer += OnCreatePlayer;
            PolyNetwork.CreatePlayer(new PolyPlayerData()
            {
                ID = $"御坂{Random.Range(10032, 20001)}号",
                position = GetRandomPosition()
            });

            Vector3 GetRandomPosition()
                => new Vector3(Random.value * 5, 1, Random.value * 5);
        }

        private void OnCreatePlayer(IPlayerData obj)
        {
            PolyPlayer player = Instantiate(playerPrefabs);
            player.playerData = (PolyPlayerData)obj;

            player.SetName(player.playerData.ID);
            player.transform.position = player.playerData.position;

            //实例化PolyInput，赋值给netInput字段
            player.netInput = new PolyInput(player.playerData.ID);

            Debug.Log($"角色：{player.playerData.ID}  位置：{player.transform.position}");
        }
    }
}